
#include "image.h"

using namespace WtfEngine;


void Image::Create(const iVector2D& vDim) {
	Destroy();
	mpImage = corona::CreateImage(vDim[0], vDim[1], corona::PF_R8G8B8A8);
	if(!mpImage){
		throw new std::runtime_error("Failed to create image");
	};
};


void Image::Read(const String& sFile) {
	Destroy();
	mpImage = corona::OpenImage(sFile.c_str(), corona::PF_R8G8B8A8);
	if(!mpImage) {
		throw new std::runtime_error(String("Failed to read image: ") + sFile);
	};
};

void Image::Write(const String& sFile) {
	if(!corona::SaveImage(sFile.c_str(), corona::FF_AUTODETECT, mpImage)) {
		throw new std::runtime_error(String("Failed to save image: ") + sFile);
	};
};


WtfEngine::IDataObject * Image::Load(const StringPool::Entry& sName) {
	Image * pImage = new Image(sName);
	pImage->Read(sName);
	return pImage;
};


void Image::Destroy() {
	if(mpImage) {
		delete mpImage;
		mpImage = NULL;
	};
};

